<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class BootstrapController extends Controller
{
     //
     public function index(Request $request){
         $data['username'] = $request->input('username');

         $where = '';
         if($data['username']) {
             $where = ' where username = '.$data['username'];
         }

        $type  = $request->input('type');
        if($type == 'ajax'){
            $offset = $request->input('offset',0);
            $limit  = $request->input('limit',0);
            $order  = $request->input('order','asc');
            $sort   = $request->input('sort','rmb');

            $sql    ="select * from robot $where order by $sort $order ";
            if($offset || $limit){
                $sql .= " limit $offset,$limit ";
            }
            $res    = DB::select($sql);
        
            $list   = array();
            $list['total'] = $request->input('total',0);
            $k =0;
            foreach ($res as $v){
                $list['rows'][$k]                   = (array)($v);
                $list['rows'][$k]['avatar']          = '<img width="60px" height="50px" src="'.('https://duimianimg.loovee.com/MediaServerMblove/servlet/Proxy/PhotoServlet/'.$v->avatar).'"/>';
                $list['rows'][$k]['dateline']       = date('Y-m-d H:i:s',$v->dateline);
                if(mb_strlen($list['rows'][$k]['remark']) > 16) {
                    $list['rows'][$k]['remark'] =
                        '<a data-toggle="remarks" data-trigger="hover" data-content="'.$list['rows'][$k]['remark'].'"
                            data-html="true" class="">'.mb_substr($list['rows'][$k]['remark'],0,16).'...'.'</a>';
                }
                $k++;
            }
            echo json_encode($list);die();
        }else{
            $res  = DB::select("select count(1) as num from robot $where " );
            $res =(array)$res;
            $data['total'] = $res[0]->num;
        }

         //数据库可更改字段
         $data['filed'] = array(
             'username'=>'',
             'avatar'=>'',
             'phone'=>'',
             'nick'=>'',
             'dateline'=>'',
             'invite_num'=>'',
             'rmb'=>'',
             'deposit'=>'',
         );
        return view('bootstrap',$data);
    }

    //
    public function index_ex(){
        return view('bootstrap_table_ex');
    }

    public function ajax(){
        $sql    ="select * from robot  ";
        $res    = DB::select($sql);
        $list   = array();
        $k =0;
        foreach ($res as $v){
            $list[$k]                   = (array)($v);
            $k++;
        }
        echo json_encode($list);die();
    }
}
